package math.geom3d;

import math.base.CCS3d;
import math.base.Point3d;
import math.base.Vector3d;
import math.geomUtils.CurveType;

/**
 * @author Saga
 *
 */
public class Ellipse3d extends ConicalCurve3d {
    public Ellipse3d(final CCS3d _pos, final double _majorRadius, final double _minorRadius) {

    }

    @Override
    public final CurveType getCurveType() {
        return CurveType.ELLIPSE;
    }

    @Override
    public final void reverse() {

    }

    @Override
    public final Curve3d reversed() {
        return null;
    }

    @Override
    public final boolean isPeriodic() {
        return true;
    }

    @Override
    public final double getPeriod() {
        return Math.PI * 2.0;
    }

    @Override
    public final double getFirstParameter() {
        return 0.0;
    }

    @Override
    public final double getLastParameter() {
        return Math.PI*2.0;
    }

    @Override
    public final Point3d calcValue(final double _param) {
        final double cosVal = Math.cos(_param) * this.majorRadius;
        final double sinVal = Math.sin(_param) * this.minorRadius;
        return this.pos.getOrigin().added(this.pos.getDirectX().multiplied(cosVal)
                .added(this.pos.getDirectY().multiplied(sinVal)));
    }

    @Override
    public final Vector3d calcDerivative(final double _param) {
        final double sinVal = Math.sin(_param) * this.majorRadius;
        final double cosVal = Math.cos(_param) * this.minorRadius;
        return this.pos.getDirectX().multiplied(-sinVal).added(
                this.pos.getDirectY().multiplied(cosVal));
    }

    @Override
    public final Vector3d calcDerivativeN(final double _param, final int _order) {
        if (_order < 1) {
            throw new ArithmeticException("");
        }

        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);

        switch (_order % 4) {
        case 0:
            return this.pos.getDirectX().multiplied(cosVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(sinVal * this.minorRadius));

        case 1:
            return this.pos.getDirectX().multiplied(-sinVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(cosVal * this.minorRadius));

        case 2:
            return this.pos.getDirectX().multiplied(-cosVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(-sinVal * this.minorRadius));


        case 3:
            return this.pos.getDirectX().multiplied(sinVal * this.majorRadius).added(
                    this.pos.getDirectY().multiplied(-cosVal * this.minorRadius));

        default:
            throw new ArithmeticException("");
        }
    }

    @Override
    public final void calcValueAndDerivative1(final double _param, final Point3d _pt,
            final Vector3d _der1) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);
        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal * this.majorRadius)
                        .added(this.pos.getDirectY().getCoord()
                                .multiplied(sinVal * this.minorRadius))));
        _der1.setCoord(
                this.pos.getDirectX().getCoord().multiplied(-sinVal * this.majorRadius).added(
                        this.pos.getDirectY().getCoord()
                                .multiplied(cosVal * this.minorRadius)));
    }

    @Override
    public final void calcValueAndDerivative2(final double _param, final Point3d _pt,
            final Vector3d _der1, final Vector3d _der2) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);
        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal * this.majorRadius)
                        .added(this.pos.getDirectY().getCoord()
                                .multiplied(sinVal * this.minorRadius))));
        _der1.setCoord(
                this.pos.getDirectX().getCoord().multiplied(-sinVal * this.majorRadius).added(
                        this.pos.getDirectY().getCoord()
                                .multiplied(cosVal * this.minorRadius)));
        _der2.setCoord(
                this.pos.getDirectX().getCoord().multiplied(-cosVal * this.majorRadius).added(
                        this.pos.getDirectY().getCoord()
                                .multiplied(-sinVal * this.minorRadius)));
    }

    @Override
    public final void calcValueAndDerivative3(final double _param, final Point3d _pt,
            final Vector3d _der1, final Vector3d _der2, final Vector3d _der3) {
        final double cosVal = Math.cos(_param);
        final double sinVal = Math.sin(_param);
        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(cosVal * this.majorRadius)
                        .added(this.pos.getDirectY().getCoord()
                                .multiplied(sinVal * this.minorRadius))));
        _der1.setCoord(
                this.pos.getDirectX().getCoord().multiplied(-sinVal * this.majorRadius).added(
                        this.pos.getDirectY().getCoord()
                                .multiplied(cosVal * this.minorRadius)));
        _der2.setCoord(
                this.pos.getDirectX().getCoord().multiplied(-cosVal * this.majorRadius).added(
                        this.pos.getDirectY().getCoord()
                                .multiplied(-sinVal * this.minorRadius)));
        _der3.setCoord(
                this.pos.getDirectX().getCoord().multiplied(sinVal * this.majorRadius).added(
                        this.pos.getDirectY().getCoord()
                                .multiplied(-cosVal * this.minorRadius)));
    }

    @Override
    public final Ellipse3d clone() {
        return new Ellipse3d(this.pos.clone(), this.majorRadius, this.minorRadius);
    }

    private double majorRadius;
    private double minorRadius;
}
